package cn.flyboy.utils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;

public class ConfigUtils {

    public static Map getConfig(HttpServletRequest request) {
        String path = returnPath(request);
        return XMLHelper.xmlToMap(path);
    }


    public static boolean setConfig(HttpServletRequest request, Map<String, String> config) throws IOException {
        String path = returnPath(request);
        return XMLHelper.updateXml(path, config);
    }

    public static String returnPath(HttpServletRequest request) {
        //..\assets\config.xml
        StringBuilder configPath = new StringBuilder();
        String folderPath = request.getSession()
                .getServletContext()
                .getRealPath(File.separator);

        configPath.append(folderPath).append(File.separator)
                .append("assets").append(File.separator)
                .append("config.xml");
        System.out.println(configPath.toString());
        return configPath.toString();
    }
}
